package com.ruoyi.project.system.erp.service;

import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONObject;
import com.ruoyi.common.utils.CacheUtils;
import com.ruoyi.common.utils.digest.Pwd;
import com.ruoyi.framework.config.URLConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import static com.ruoyi.common.constant.ShiroConstants.ACCESSTOKEN_CACHE;

@Service
@Slf4j
public class TokenServiceImpl implements ITokenService {

    @Resource
    URLConfig urlConfig;

    @Override
    public void accessToken() {
//        log.info("*****************开始调用第三方接口获取access_token*************************");
//        JSONObject obj = new JSONObject();
//        obj.put("username", Pwd.getPwd(urlConfig.getRsaKey(), urlConfig.getUsername()));
//        obj.put("password", Pwd.getPwd(urlConfig.getRsaKey(), urlConfig.getPassword()));
//        String testStr = obj.toString();
//        String result2 = HttpRequest.post(urlConfig.getGet_access_token())
//                .header("Content-Type", "application/json")
//                .header("sign", Pwd.getSign(urlConfig.getKey(), testStr))
//                .body(testStr).execute().body();
//        log.info("获取access_token成功：{}", result2);
//        JSONObject jsonObject = new JSONObject(result2);
//        log.info("获取access_token状态：{}", jsonObject.getInt("state"));
//        if(1 == jsonObject.getInt("state")){
//            CacheUtils.put(ACCESSTOKEN_CACHE,ACCESSTOKEN_CACHE,jsonObject.getStr("data"));
//            log.info("设置access_token缓存：{}", CacheUtils.getACCESSTOKEN_CACHE(ACCESSTOKEN_CACHE));
//        }
        CacheUtils.put(ACCESSTOKEN_CACHE, ACCESSTOKEN_CACHE, "eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJURVNUMTA0OTQiLCJpc3MiOiJTQ1AiLCJpYXQiOjE1ODY3MDY3ODUsImV4cCI6MTU4Njc5MzE4NX0.35G7Zq5Ik-34Y6qy9eCf43w3DMqXasOakRWw8YznVRF7lkQIv2y57OFIy__HnV6702EcsfPEpeNcOkanhz1WYQ");
        log.info("设置access_token缓存：{}", (String) CacheUtils.getACCESSTOKEN_CACHE(ACCESSTOKEN_CACHE));
    }

    @Override
    public String getList(String url, String obj) {
        log.info("请求URL：{} 请求参数：{}", url, obj);

        String result2 = HttpRequest.post(url)
                .header("Content-Type", "application/json")
                .header("sign", Pwd.getSign(urlConfig.getKey(), obj))
                .header("access_token", (String) CacheUtils.getACCESSTOKEN_CACHE(ACCESSTOKEN_CACHE))
                .body(obj).execute().body();
        log.info("第三方返回结果：{}", result2);

        if (result2.indexOf("\"state\":2") > -1) {
            JSONObject jsonObject = new JSONObject(result2);
            log.error("出错了,{}", jsonObject.getStr("msg"));
            return jsonObject.getStr("msg");
        }
        //容错，尝试3次
        if (result2.indexOf("\"state\":1") < 0) {
            Integer count = (Integer) CacheUtils.get("count");
            if (null == count) {
                count = 0;
            }
            if (count < 2) {
                accessToken();
                CacheUtils.put("count", count + 1);
                getList(url, obj);
            }
            return "出错啦";
        }

        JSONObject jsonObject = new JSONObject(result2);
        return jsonObject.getStr("data");
    }
}
